const Actions = require('./actions');
import { Storage, monStorage } from 'clientConfig/util/StoreData';
import urllist from 'clientConfig/urllist';
import { Control } from 'react-keeper'
const net = require('clientConfig/util/net');
const { Toast } = SaltUI;
const ui = require('clientConfig/util/jsapi/ui')
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {
        tel: '',
        code: ''
    },

    onFetch: function (params, cb) {

    },
    onSumbmit: function (params, cb) {
        let t = this;
        //3.
        let name = monStorage.get('user.nickname')
        let email = monStorage.get('user.email')
        let avatar = monStorage.get('user.avatar')
        let unionid = Storage.get('user.unionid')
        let corp_name = monStorage.get('user.corp_name')
        let auth_level = monStorage.get('user.auth_level')
        let bpPassword = monStorage.get('user.bpPassword')
        let bpPortalID = monStorage.get('user.bpPortalID')
        let bpResourceName = monStorage.get('user.bpResourceName')
        let bpUserGUID = Storage.get('user.unionid')
        let bpUserID = Storage.get('user.nickname')
        let type = window.type;
        let id = unionid;
        let { tel, code } = params;
        this.data.tel = tel;
        this.data.code = code;
        let body = type == 'bp' ? { bpPassword, bpPortalID, bpResourceName, bpUserGUID, bpUserID } : { name, email, avatar, unionid, corp_name, auth_level };
        let url = urllist.checkverifycode + "?tel=" + this.data.tel + '&code=' + this.data.code + '&type=' + type + '&id=' + id;
        net.fetchUrl(url, { method: 'post', body: { ...body } }).then((jsondata) => {
            // alert(JSON.stringify(jsondata))
            console.log(JSON.stringify(jsondata))

            if (jsondata.errcode == 0) {
                Toast.show({
                    type: 'success',
                    content: '验证成功'
                });
                Storage.set('user.id',jsondata.user.id);
                monStorage.set('user.session', jsondata.session);
                Storage.set('user.tel', tel);
                // Storage.set('user.info', jsondata);
                // Control.go('/bind/modify')
                if (jsondata.new_user) {
                    Storage.set('user.tel', tel);
                    Storage.set('user.info', jsondata);
                    Control.go('/bind/modify/fill')
                } else {
                    // Control.go('/login', jsondata);
                    // Control.go('/home', jsondata);
                    return jsondata;
                }
            } else if (jsondata.errcode == 4112) {
                Toast.show({
                    type: 'error',
                    content: '手机号错误'
                });
            } else if (jsondata.errcode == 4113) {
                Toast.show({
                    type: 'error',
                    content: '验证码超时'
                });
            } else if (jsondata.errcode == 4114) {
                Toast.show({
                    type: 'error',
                    content: '验证码不匹配'
                });
            } else if (jsondata.errcode == 4115) {
                Toast.show({
                    type: 'error',
                    content: '手机号已绑定，重新绑定'
                });
                t.data.tel = '';
                t.data.code = '';
                t.updateComponent();
            } else if (jsondata.errcode == 4116) {
                Toast.show({
                    type: 'error',
                    content: '错误的第三方平台类型'
                });
            } else if (jsondata.errcode == 4118) {
                Toast.show({
                    type: 'error',
                    content: '无效的第三方信息'
                });
                ui.close();
            }else if (jsondata.errcode == 1){
                Toast.show({
                    type: 'error',
                    content: '操作过于频繁，稍后再试'
                });
            }
        }).then((result)=>{
            if (result) {
                let url = urllist.is_bind + '?id=' + Storage.get('user.id');
                net.fetchUrl(url).then((jsondata) => {
                    if (jsondata.errcode == 0) {
                        result.role = { text: jsondata.bind_info };
                        Control.go('/home', {result})
                    } else {
                        Control.go('/bind/userbind', result);
                    }
                });
            }
        });
    },
    updateComponent: function () {
        this.trigger(this.data);
    },

    getInitialState: function () {
        return this.data;
    }
});
